package Day011_多线程1.demo4_volatile关键字;

/**
 * @author zhangyan
 * @date 2020/4/28 14:26
 */
public class volatile解决可见性 {
    public static void main(String[] args) {
        //创建子线程
        MyThread mt = new MyThread();
        mt.start();
        //主线程
        while (true) {
            if (MyThread.flag == true) {
                System.out.println("死循环结束");
                break;
            }
        }

    }
}

class MyThread extends Thread {
    volatile static boolean flag = false; //主线程 子线程共享的变量

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println("flag修改后的值:" + flag);
    }
}


